package com.zrh.jsd.common.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

//加入@EnableOpenApi注解
@EnableOpenApi
@Configuration
@EnableKnife4j
public class Swagger3Config {
    @Bean
    public Docket createRestApi1() {
        // 指定swagger3.0版本
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo1())
                .groupName("开发小组一")
                // 关闭之后在页面上不显示。开发环境才会用到swangger，正式环境需要关闭swagger
                .enable(true)
                // 使用过滤必须先调用select
                .select()
                // 指定扫描的包路径 ，常用方式;路径是向后模糊匹配，也会扫描到com.zrh.jsd.web.controllerex
                // basePackage可以根据包路径来生成特定类的API,any方法时默认所有都有效，none方法都无效  -- 一般用这个
                // withClassAnnotation根据类注解，withMethodAnnotation是根据方法注解
                .apis(RequestHandlerSelectors.basePackage("com.zrh.jsd.web.controller"))
                //只扫描controller
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                //加了ApiOperation注解的方法才会程程api
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 一般用ant匹配请求路径，any是匹配任意路径，none是都不匹配，regrex是正则匹配
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo1() {
        return new ApiInfoBuilder()
                .title("Swagger3接口文档 zrh")
                .description("更多请咨询服务开发者zrh。")
                .contact(new Contact("zrh", "http://www.baidu.com", "364656145@qq.com"))
                .version("1.0")
                .build();
    }

    @Bean
    public Docket createRestApi2() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("开发小组二")
                .apiInfo(apiInfo2())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zrh.jsd.web.excontroller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo2() {
        return new ApiInfoBuilder()
                .title("Swagger3接口文档 zrh")
                .description("更多请咨询服务开发者zrh。")
                .contact(new Contact("zrh", "http://www.baidu.com", "364656145@qq.com"))
                .version("1.0")
                .build();
    }

}